package com.xw.controller.xw;

import java.math.BigDecimal;
import java.net.URLDecoder;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.fusioncharts.exporter.generators.PDFGenerator;
import com.xw.controller.base.BaseController;
import com.xw.entity.Page;
import com.xw.entity.system.User;
import com.xw.entity.xw.Datas;
import com.xw.entity.xw.Drug;
import com.xw.entity.xw.Feedback;
import com.xw.entity.xw.Kc;
import com.xw.service.xw.drug.DrugService;
import com.xw.util.AppUtil;
import com.xw.util.ChineseCharToEn;
import com.xw.util.Const;
import com.xw.util.PageData;

/**
 * 类名称: DrugController
 * 类描述: 药品controller
 * @author : jeff
 * 
 */
@Controller
@RequestMapping(value = "/drug")
public class DrugController extends BaseController{
	
		
	@Resource(name = "drugService")
	private DrugService drugService;
	
	
		/**
		 * 药品资料列表
		 * @param request
		 * @return
		 * @throws Exception
		 */
		@RequestMapping(value = "/list")
		public ModelAndView list(Page page) throws Exception{
			ModelAndView mv = new ModelAndView();
			PageData pd = new PageData();
			pd = this.getPageData();
			String gjz = pd.getString("gjz");
						
			Subject currentUser = SecurityUtils.getSubject();//用户Id,session里面获取
			Session session = currentUser.getSession();
			User user = new User();
			user = (User)session.getAttribute(Const.SESSION_USER);
			long mzId =user.getMzid();
			pd.put("mzId",mzId);
			System.out.println("门诊id门诊id门诊id门诊id门诊id："+mzId);
			pd.put("gjz",gjz);
			page.setPd(pd);
			List<PageData> listAll  =  drugService.getList(page);
			System.out.println("listAlllistAlllistAlllistAll:"+listAll.size());
			
			mv.addObject("listAll", listAll);
			mv.setViewName("xw/drug/drug_list");
			return mv;
		}

		
		/**
		 * 添加药品资料界面
		 * @param request
		 * @return
		 * @throws Exception
		 */
		@RequestMapping(value = "/goAdd")
		public ModelAndView goAdd() throws Exception{
			ModelAndView mv = new ModelAndView();
			PageData pd = this.getPageData();
			
			List<Datas> listYplx = drugService.listYplx();
			List<Datas> listYpdw = drugService.listYpdw();	
			System.out.println("药品类型："+listYplx);
			mv.addObject("listYplx", listYplx);
			mv.addObject("listYpdw",listYpdw);
			mv.setViewName("xw/drug/adddrug");
			return mv;
		}
		
		
		/**
		 * 新增药品资料
		 * @param session
		 * @throws Exception
		 */
		@RequestMapping(value="/add")
		public ModelAndView add() throws Exception {
			ModelAndView mv = this.getModelAndView();
			PageData pd = this.getPageData();
			Drug drug=new Drug();
			
			String ypCode = pd.getString("ypbh");
			drug.setYpCode(ypCode);
			String ypName = pd.getString("ypmc");
			drug.setYpName(ypName);
			ChineseCharToEn cte = new ChineseCharToEn();  
	        String ypSpell = cte.getAllFirstLetter(ypName);
			drug.setYpSpell(ypSpell);
			String ypType = pd.getString("yplx");
			drug.setYpType(ypType);
			String ypUnit = pd.getString("dw");
			drug.setYpUnit(ypUnit);
			String price = pd.getString("dj");
			drug.setPrice(new BigDecimal(price));
			String ypUsage = pd.getString("ypyf");
			drug.setYpUsage(ypUsage);
			drugService.add(drug);

			Drug drug2=new Drug();
			pd.put("ypCode",ypCode);
			drug2 = drugService.findYp(pd);
			Kc kc = new Kc();
			Subject currentUser = SecurityUtils.getSubject();//用户Id,session里面获取
			Session session = currentUser.getSession();
			User user = new User();
			user = (User)session.getAttribute(Const.SESSION_USER);
			long mzId =user.getMzid();
			kc.setMzId(mzId);
			kc.setYpId(drug2.getId());
			kc.setKcTotal(new BigDecimal(0));
			drugService.addkc(kc);
			
			
			mv.addObject("msg", "success");
			mv.setViewName("save_result");
			return mv;
		}
		
		
		/**
		 * 修改药品资料信息页面
		 * @param session
		 * @return
		 * @throws Exception
		 */
		
		@RequestMapping(value = "/goEdit")
		public ModelAndView goEdit(HttpServletRequest request) throws Exception{
			ModelAndView mv = new ModelAndView();
			PageData pd =  this.getPageData();
			
			String id = request.getParameter("id");
			id = URLDecoder.decode(id, "UTF-8");
			pd.put("id", id);
					
			List<Datas> listYplx = drugService.listYplx();
			List<Datas> listYpdw = drugService.listYpdw();	
			mv.addObject("listYplx", listYplx);
			mv.addObject("listYpdw",listYpdw);
			Drug drug=drugService.getOne(pd);
			mv.addObject("drug",drug);
			mv.setViewName("xw/drug/editdrug");
			return mv;
		}
		
		
		/**
		 * 保存修改药品资料
		 * @param session
		 * @throws Exception
		 */
		@RequestMapping(value="/edit")
		public ModelAndView edit() throws Exception {
			ModelAndView mv = this.getModelAndView();
			PageData pd = this.getPageData();
			Drug drug=new Drug();
			
			/*String ypCode = pd.getString("ypbh");
			drug.setYpCode(ypCode);*/
			String id = pd.getString("id");
			drug.setId(Long.valueOf(id));
			String ypCode = pd.getString("ypbh");
			drug.setYpCode(ypCode);
			String ypName = pd.getString("ypmc");
			drug.setYpName(ypName);
			ChineseCharToEn cte = new ChineseCharToEn();  
	        String ypSpell = cte.getAllFirstLetter(ypName);
			drug.setYpSpell(ypSpell);
			String ypType = pd.getString("yplx");
			drug.setYpType(ypType);
			String ypUnit = pd.getString("dw");
			drug.setYpUnit(ypUnit);
			String price = pd.getString("dj");
			drug.setPrice(new BigDecimal(price));
			String ypUsage = pd.getString("ypyf");
			drug.setYpUsage(ypUsage);
			drugService.edit(drug);
			mv.addObject("msg", "success");
			mv.setViewName("save_result");
			return mv;
		}
		
		
		/**
		 * 删除药品资料
		 * @param response
		 * @param session
		 * @throws Exception 
		 */
		@ResponseBody
		@RequestMapping(value="/delete")
		public void delete(HttpServletRequest request) throws Exception{
			PageData pd = this.getPageData();
			String id = request.getParameter("id");
			
			pd.put("id",id);
			drugService.delete(pd);
		}
		
		
		
		/**
		 * 批量删除
		 * @throws Exception 
		 */
		@RequestMapping(value = "/deleteAll")
		@ResponseBody
		public Object deleteAll() throws Exception {
			PageData pd = new PageData();
			Map<String, Object> map = new HashMap<String, Object>();

				pd = this.getPageData();
				List<PageData> pdList = new ArrayList<PageData>();
				String USER_IDS = pd.getString("USER_IDS");
				System.out.println("USER_IDS:"+USER_IDS);

				if (null != USER_IDS && !"".equals(USER_IDS)) {
					String ArrayUSER_IDS[] = USER_IDS.split(",");
					drugService.deleteAll(ArrayUSER_IDS);
					pd.put("msg", "ok");
				} else {
					pd.put("msg", "no");
				}

				pdList.add(pd);
				map.put("list", pdList);
			return AppUtil.returnObject(pd, map);
		}
		
		
		/**
		 * 判断药品编号是否存在
		 * @throws Exception 
		 */
		@RequestMapping(value = "/hasYp")
		@ResponseBody
		public Object hasYp(HttpServletRequest request) throws Exception {
			Map<String, String> map = new HashMap<String, String>();
			String errInfo = "success";
			PageData pd = new PageData();
			String ypCode = request.getParameter("ypbh");
			System.out.println("药品编号药品编号："+ypCode);
			pd.put("ypCode",ypCode);
			if (drugService.findYp(pd) == null) {
				errInfo = "error";
			}
			map.put("result", errInfo); // 返回结果
			return AppUtil.returnObject(new PageData(), map);
		}
		
		
}
